/**
 * vuex 插件，用于localStorage持久化存储
 * @param {object} store 
 */
import {
    setItem,
    getItem,
    removeItem
} from '../../utils/storage.js'

const storage = store => {
    
    store.commit('login', getItem('user'));
    store.commit('setMsgList', getItem('msgList'));

    // 当 store 初始化后调用
    store.subscribe((mutation, state) => {
        switch (mutation.type) {
            case 'login':
                // 持久化登录信息
                console.log("持久化登录信息", state.user);
                setItem('user', state.user)
                break;

            case 'logout':
                // 清空登录信息
                removeItem('user')
                removeItem('msgList')
                break;
            case 'addNewFriend': 
              setItem('msgList', state.msgList)
            case 'sendMsgSuccess':
              setItem('msgList', state.msgList)
            case 'setMsgList': 
              console.log('进来了setMsgList')
              setItem('msgList', state.msgList)
            case 'resetUnreadMsg':
              setItem('msgList', state.msgList)
            case 'editNewMsg':
              console.log('进来了editNewMsg')
              console.log(state.msgList)
              setItem('msgList', state.msgList)
              break;
            default:
                break;
        }
    })
}

export default storage;
